/**
 * 服务器端主线程工作：接收消息，放在接收队列里
 * */
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.concurrent.LinkedBlockingQueue;

public class Server {
    static LinkedBlockingQueue<DatagramPacket> receiveQueue = new LinkedBlockingQueue<>(16);
    static LinkedBlockingQueue<DatagramPacket> sendQueue = new LinkedBlockingQueue<>(16);
    static int c_count=0;//访问数量
    static int add_count=0;
    static int sub_count=0;
    static int mul_count=0;
    static int div_count=0;
    static DatagramSocket socket;
    public static void main(String[] args) throws SocketException {
        int serverPort = 6789;//服务器接收端口
        socket=new DatagramSocket(serverPort);
        SendThread sendThread = new SendThread();
        sendThread.start();
        int n=8;//n为工作线程数量，通过设置来支持垂直扩展性
        for(int i=0;i<n;i++){
            CalculateThread calculateThread = new CalculateThread();
            calculateThread.start();
        }
        //DatagramSocket socket = null;
        try{
            //socket=new DatagramSocket(serverPort);
            while (true){
                byte[] buffer = new byte[1024];
                DatagramPacket requestPacket = new DatagramPacket(buffer, buffer.length);
                socket.receive(requestPacket);//收请求包
                c_count++;
                System.out.println("已经收到了了 "+c_count+"个客户端的访问请求");
                receiveQueue.add(requestPacket);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(socket!=null)
            socket.close();
        }
    }
}
